<template>
  <a-modal
    :title="$t('批量修改')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :okText="$t('确定')"
    @cancel="handleCancel"
    :cancelText="$t('关闭')"
    v-drag
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="dataForm" :rules="rules">
        <a-form-model-item :label="$t('操作类型')" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14 }" prop="optionType">
          <a-select v-model="dataForm.optionType" allowClear>
            <a-select-option v-for="item in optionType" :key="item.value" :value="item.value">{{
              item.title
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          :label="$t('业务员')"
          :labelCol="{ span: 6 }"
          :wrapperCol="{ span: 14 }"
          v-if="dataForm.optionType == 1"
          prop="salesId"
        >
          <a-select
            v-model="dataForm.salesId"
            allowClear
            show-search
            :filter-option="false"
            :not-found-content="null"
            @search="handleSearch"
          >
            <a-select-option v-for="item in memberList" :key="item.id" :value="item.id" @click="handleMember(item)">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <div v-if="dataForm.optionType == 2">{{ $t('本次要释放的数量：') }}{{ delectList.length }}{{ $t('个') }}</div>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import { getAction, httpAction } from '@/api/manage'

export default {
  name: 'BatchEditSalesModal',
  components: {},
  data() {
    return {
      editState: '',
      saveState: '',
      title: this.$t('操作'),
      width: 600,
      visible: false,
      model: {},
      goodsIdList: [],
      confirmLoading: false,
      reultList: [],
      dataForm: {
        optionType: '',
        salesId: '',
      },
      addList: [],
      delectList: [],
      queryList: [],
      optionType: [],
      memberList: [],
      rules: {
        optionType: [{ required: true, message: this.$t('请选择操作类型') }],
        salesId: [{ required: true, message: this.$t('请选择业务员') }],
      },
    }
  },
  methods: {
    edit(addList, delectList) {
      this.addList = addList
      this.delectList = delectList
      this.dataForm = {
        optionType: '',
        salesId: '',
      }
      this.visible = true
      initDictOptions('bis_customer_manage.operation_type').then((res) => {
        if (res.success) {
          this.optionType = res.result
        }
      })
      this.getSalemanList()
    },
    getSalemanList(value) {
      let query = {
        pageNo: 1,
        pageSize: 10,
      }
      if (value) {
        query.name = ',' + value + ','
      }
      getAction('/member/bisSalesman/list', query).then((res) => {
        if (res.success) {
          this.memberList = res.result.records
        }
      })
    },
    handleSearch(value) {
      this.getSalemanList(value)
    },
    handleMember(item) {
      this.dataForm.salesId = item.id
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      console.log(that.dataForm)
      // if (that.dataForm.optionType == '') {
      //   that.$message.warn('请选择需要修改的类型')
      //   return
      // } else {
      //   if (that.dataForm.optionType == 1 && that.dataForm.salesId == '') {
      //     that.$message.warn('请选择业务员')
      //     return
      //   }
      // }
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url = ''
          let methods = ''
          let list = []
          let query = {}
          if (this.dataForm.optionType == 1) {
            url = '/business/customerManage/addList'
            methods = 'post'
            that.addList.forEach((col) => {
              let obj = {
                memberId: col,
              }
              list.push(obj)
            })
            query.list = list
            query.salesmanId = that.dataForm.salesId
          } else if (that.dataForm.optionType == 2) {
            url = '/business/customerManage/delete'
            methods = 'delete'
            that.delectList.forEach((col) => {
              let obj = {
                id: col,
              }
              list.push(obj)
            })
            query = list
          }
          httpAction(url, query, methods).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.visible = false
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style>
.batchForm .ant-form-item-label {
  width: 30%;
}
.batchForm .ant-form-item-control-wrapper {
  width: 62.5%;
}
</style>